package com.ldn.common.basic.enums.mqtt;

import com.ldn.common.basic.enums.EnumMap;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Mqtt消息类型枚举
 *
 * @author 李大牛家的牛 (scafel@163.com)
 * @product serverless
 * @className MMessageTypeEnums
 * @createDate 2025/3/6 - 8:43
 * @description Mqtt消息类型枚举
 **/
@Getter
public enum MMessageTypeEnums {
    /**
     * 上线
     */
    ONLINE(0, "上线"),
    /**
     * 下线
     */
    OFFLINE(1, "下线"),
    /**
     * 消息
     */
    MESSAGE(2, "消息"),
    /**
     * 认证
     */
    AUTHENTICATE(3, "认证"),
    /**
     * 订阅
     */
    SUBSCRIBE(4, "订阅"),
    /**
     * 取消订阅
     */
    UNSUBSCRIBE(5, "取消订阅"),
    /**
     * 发布验证
     */
    PUBLISH_VALID(6, "发布验证"),
    /**
     * 订阅验证
     */
    SUBSCRIBE_VALID(7, "订阅验证"),
    /**
     * 未知
     */
    UNKNOWN(99, "未知"),
    ;
    private static final Map<Integer, MMessageTypeEnums> codeToEnumMap = new ConcurrentHashMap<>();
    private static final ArrayList<EnumMap> enumMap = new ArrayList<>();

    static {
        for (MMessageTypeEnums type : MMessageTypeEnums.values()) {
            codeToEnumMap.put(type.getCode(), type);
            EnumMap map = new EnumMap();
            map.setKey(type.getCode());
            map.setLabel(type.getMessage());
            enumMap.add(map);
        }
    }

    private final int code;
    /**
     * 自定义描述
     **/
    private final String message;

    MMessageTypeEnums(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static MMessageTypeEnums getType(int code) {
        MMessageTypeEnums type = codeToEnumMap.get(code);
        if (type == null) {
            return MMessageTypeEnums.UNKNOWN;
        }
        return type;
    }

    /**
     * 获取枚举类中所有的枚举值
     * @return Map
     */
    public static ArrayList<EnumMap> getEnumsMap() {
        return enumMap;
    }

}
